package com.shop.loan.handler;

import com.shop.loan.mapper.LoanOrderByMybatisMapper;
import com.shop.loan.mapper.UserBankByMybatisMapper;
import com.shop.loan.mapper.UserByMybatisMapper;
import com.shop.loan.model.UserBank;
import com.shop.loan.model.entity.SettingByMybatis;
import com.shop.loan.model.entity.UserBankByMybatis;
import com.shop.loan.model.entity.UserByMybatis;
import com.shop.loan.model.enums.OpFailType;
import com.shop.loan.model.vo.UserBankDelVo;
import com.shop.loan.redis.RedisCache;
import com.shop.loan.rentaldata.RentalPayUtil;
import com.shop.loan.util.OpFailUtil;
import com.shop.loan.util.PasswordUtil;
import com.shop.loan.util.Util;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import xyz.erupt.annotation.fun.OperationHandler;
import xyz.erupt.upms.service.EruptUserService;
import xyz.erupt.upms.util.IpUtil;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Service
public class UserBankDelHandler implements OperationHandler<UserBank, UserBankDelVo> {

    @Resource
    EruptUserService eruptUserService;

    @Resource
    OpFailUtil opFailUtil;

    @Resource
    private HttpServletRequest request;

    @Resource
    RedisCache redisCache;

    @Resource
    LoanOrderByMybatisMapper loanOrderByMybatisMapper;

    @Resource
    UserBankByMybatisMapper userBankByMybatisMapper;

    @Resource
    UserByMybatisMapper userByMybatisMapper;

    @Resource
    RentalPayUtil rentalPayUtil;

    @Resource
    Util util;

    @Override
    public String exec(List<UserBank> data, UserBankDelVo userBankDelVo, String[] param) {
        UserBank userBank = data.get(0);
        /**
         * 校验还款密码
         */
        SettingByMybatis setting = redisCache.getSetting(false);
        if (!PasswordUtil.checkPassword(userBankDelVo.getPassword(), setting.getPayPassword())) {
            opFailUtil.opFailOp(IpUtil.getIpAddr(request), eruptUserService.getCurrentUid(), OpFailType.pay);
            return "msg.error('无效还款密码!')";
        }

        Integer orderNum = loanOrderByMybatisMapper.selectIngByUser(userBank.getUserId());
        if (orderNum != null && orderNum > 0) {
            return "msg.error('用户存在审核中或进行中订单!')";
        }

        if (StringUtils.hasLength(userBank.getProtocol())) {
            rentalPayUtil.unSign(util.getUserId(userBank.getUserId()), userBank.getProtocol());
        }

        UserBankByMybatis userBankByMybatis = new UserBankByMybatis();
        userBankByMybatis.setId(userBank.getId());
        userBankByMybatis.setDeleteTime(new Date());
        userBankByMybatis.setIsDelete(true);
        userBankByMybatisMapper.updateByPrimaryKeySelective(userBankByMybatis);

        List<UserBankByMybatis> userBankByMybatisList = userBankByMybatisMapper.selectByUser(userBank.getUserId());
        if (userBankByMybatisList == null || userBankByMybatisList.size() == 0) {
            UserByMybatis userByMybatis = new UserByMybatis();
            userByMybatis.setId(userBank.getUserId());
            userByMybatis.setBankAuthStatus(false);
            userByMybatisMapper.updateByPrimaryKeySelective(userByMybatis);
        }
        return null;
    }
}
